# encoding=utf-8
import time

import cv2
import numpy as np


def time_wrapper(f, *args, **kwargs):
    t1 = time.perf_counter()
    res = f(*args, **kwargs)
    t2 = time.perf_counter()
    print("{}, time: {}s".format(f, t2 - t1))
    return res


def crop_image_from_gray(img, tol=7):
    if img.ndim == 2:
        mask = img > tol
        return img[np.ix_(mask.any(1), mask.any(0))]
    elif img.ndim == 3:
        # 先将图片转换成灰度
        gray_img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
        # 设置遮罩，255为纯白色， 0为纯黑色
        # 其实这个mask是过滤掉一些黑色像素
        mask = gray_img > tol

        # np.ix_([a1,a2,a3,...],[b1,b2,b3,...]): 讲一个数组 1、选取其中的a1,a2,a3列， 然后将每列元素以b1,b2,b3方式重新排列
        check_shape = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))].shape[0]
        if (check_shape == 0):  # image is too dark so that we crop out everything,
            return img  # return original image
        else:
            img1 = img[:, :, 0][np.ix_(mask.any(1), mask.any(0))]
            img2 = img[:, :, 1][np.ix_(mask.any(1), mask.any(0))]
            img3 = img[:, :, 2][np.ix_(mask.any(1), mask.any(0))]
            #         print(img1.shape,img2.shape,img3.shape)
            img = np.stack([img1, img2, img3], axis=-1)
        #         print(img.shape)
        return img


def circle_crop(img, sigmaX=30):
    """
    Create circular crop around image centre
    """

    img = cv2.imread(img)
    img = crop_image_from_gray(img)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

    height, width, depth = img.shape

    x = int(width / 2)
    y = int(height / 2)
    r = np.amin((x, y))

    circle_img = np.zeros((height, width), np.uint8)
    cv2.circle(circle_img, (x, y), int(r), 1, thickness=-1)
    # bitwise_and 来裁剪原始图像，得到一个圆形图像
    img = cv2.bitwise_and(img, img, mask=circle_img)
    img = crop_image_from_gray(img)
    img = cv2.addWeighted(img, 4, cv2.GaussianBlur(img, (0, 0), sigmaX), -4, 128)  # ##
    # print(img.shape)

    return img


def preprocess(img):
    return cv2.resize(circle_crop(img), (224, 224), interpolation=cv2.INTER_LINEAR)


if __name__ == '__main__':
    image_src = ["t1.jpeg", "t2.jpeg"]
    for e_s in image_src:
        out = time_wrapper(preprocess, e_s)
        cv2.imwrite(e_s.replace(".jpeg", ".png"), out)
